#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
配置检查脚本
用于检查研究助理相关的配置是否正确
"""

import os
import sys
from src.config.research_settings import ResearchSettings

# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, project_root)

def check_env_vars():
    """检查环境变量配置"""
    print("🔍 检查环境变量配置...")
    
    required_vars = [
        "RESEARCH_OPENAI_API_KEY",
        "RESEARCH_OPENAI_API_BASE", 
        "RESEARCH_OPENAI_MODEL"
    ]
    
    optional_vars = [
        "RESEARCH_OPENAI_MAX_TOKENS",
        "RESEARCH_OPENAI_TEMPERATURE",
        "RESEARCH_WORKFLOW_TIMEOUT"
    ]
    
    all_good = True
    
    # 检查必需的环境变量
    for var in required_vars:
        value = os.getenv(var)
        if not value:
            print(f"❌ 缺少必需的环境变量: {var}")
            all_good = False
        else:
            # 对于密钥，只显示部分信息
            if "KEY" in var:
                print(f"✅ {var}: {'*' * (len(value)-4) + value[-4:]}")
            else:
                print(f"✅ {var}: {value}")
    
    # 检查可选的环境变量
    for var in optional_vars:
        value = os.getenv(var)
        if value:
            print(f"ℹ️  {var}: {value}")
    
    return all_good

def check_research_settings():
    """检查研究设置"""
    print("\n🔍 检查研究设置...")
    
    try:
        # from src.config.research_settings import ResearchSettings
        settings = ResearchSettings()
        
        print(f"✅ API Base: {settings.OPENAI_API_BASE}")
        print(f"✅ Model: {settings.OPENAI_MODEL}")
        print(f"✅ Max Tokens: {settings.OPENAI_MAX_TOKENS}")
        print(f"✅ Temperature: {settings.OPENAI_TEMPERATURE}")
        
        return True
    except Exception as e:
        print(f"❌ 研究设置检查失败: {e}")
        return False

def main():
    """主函数"""
    print("🔧 研究助理配置检查工具")
    print("=" * 50)
    
    env_ok = check_env_vars()
    settings_ok = check_research_settings()
    
    print("\n" + "=" * 50)
    if env_ok and settings_ok:
        print("✅ 所有配置检查通过")
        print("现在可以尝试运行研究任务了")
    else:
        print("❌ 配置检查失败")
        print("请检查上述错误并修正配置")
        sys.exit(1)

if __name__ == "__main__":
    main()